using UnityEngine;
using UnityEngine.UI;

[RequireComponent(typeof(Button))]
public class LanguageButton : MonoBehaviour
{
    private Button button;
    private Text buttonText;

    void Awake()
    {
        button = GetComponent<Button>();
        buttonText = GetComponentInChildren<Text>();

        // 添加点击事件监听
        button.onClick.AddListener(OnButtonClick);

        // 初始文本
        UpdateButtonText();
    }

    void OnButtonClick()
    {
        LanguageManager.Instance.ToggleLanguage();
    }

    public void UpdateButtonText()
    {
        if (buttonText != null && LanguageManager.Instance != null)
        {
            // 显示当前语言的相反语言名称（提示用户点击后会切换到什么语言）
            var nextLang = (LanguageManager.Instance.currentLanguage == SystemLanguage.Chinese ||
                           LanguageManager.Instance.currentLanguage == SystemLanguage.ChineseSimplified)
                           ? SystemLanguage.English
                           : SystemLanguage.Chinese;

            buttonText.text = nextLang == SystemLanguage.English ? "中文" : "English";
        }
    }

    void OnDestroy()
    {
        if (button != null)
        {
            button.onClick.RemoveListener(OnButtonClick);
        }
    }
}